<html>
<o:MultiLanguage value="FRONTMULTILANGUAGETAG">

  <head>
    <title>部门选择框</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <link rel="stylesheet" href="../css/main-front.css" type="text/css">
    <link rel="stylesheet" href="../css/dtree.css">
    <script src="../js/util.js"></script>
    <script src="../js/dtree.js"></script>

    <script src="../js/dtree-user.js"></script>
    <script src="../js/jquery-1.8.3.js"></script>
    <script type="text/javascript" src="../js/other/jquery.artDialog.source.js?skin=aero"></script>
    <script type="text/javascript" src="../js/other/iframeTools.source.js"></script>
    <script type="text/javascript" src="../js/other/jquery-framedialog.js"></script>
    <script src="../js/other/obpm-jquery-bridge.js"></script>
    <script type="text/javascript" src="../js/other/jquery.cookie.js"></script>
    <script type="text/javascript" src="../js/other/jquery.hotkeys.js"></script>
    <script type="text/javascript" src="../js/other/jquery.jstree.js"></script>
    <script type="text/javascript" src="../js/other/jquery.jstree.checkbox.js"></script>
    <script type="text/javascript" src="../js/other/json2.js"></script>
    <script type="text/javascript" src="../js/other/jquery.slimscroll.min.js"></script>

    <script language="JavaScript">
      var args = OBPM.dialog.getArgs();
      var parentObj = args['parentObj'];
      /*存放deptid*/
      var targetid = args['targetid'];
      /*存放deptname*/
      var viewName = args['receivername'];
      //是否重复
      function isDuplicate(values, value) {
        for (var i = 0; i < values.length; i++) {
          if (values[i] == value) {
            return true;
          }
        }
        return false;
      }

      function doReturn() {

        var sis = document.getElementsByName("_selectitem");

        var array = new Array();
        if (sis != null && sis.length > 0) {
          for (var i = 0; i < sis.length; i++) {
            var e = sis[i];
            if (e.type == 'checkbox') {
              if (e.checked && e.value) {
                var rtn = {};
                rtn.text = jQuery(e).attr("text");
                rtn.value = e.value;
                array.push(rtn);
              }
            }
          }
        }
        OBPM.dialog.doReturn(array);


      }


      function doInit() {
        if (args) {
          var value = args.value;
          var readonly = args.readonly;

          // 只读情况下，隐藏按钮条
          if (readonly) {
            var btnbarDiv = document.getElementById("btnbarDiv");
            btnbarDiv.style.display = "none";
          }

          if (value) {
            var sis = document.getElementsByName("_selectitem");
            var str = value;

            var checkedArray = str.split(";");
            var temp = [];
            for (var i = 0; i < checkedArray.length; i++) {
              if (sis) {
                for (var j = 0; j < sis.length; j++) {
                  var e = sis[j];
                  if (!isDuplicate(temp, e.value)) {
                    toggleCheck(e, checkedArray);
                    temp.push(e.value);
                  }
                }
              }
            }

          }
        }
      }

      function toggleCheck(oEl, checkedValues) {
        for (var i = 0; i < checkedValues.length; i++) {
          if (oEl.value == checkedValues[i] && oEl.value != '') {
            oEl.checked = true;
            d.openTo(checkedValues[i]);
          }
        }
      }

      function doSelect(rtn) {
        var array = new Array();
        array.push(rtn);

        OBPM.dialog.doReturn(array);
      }

      function setDivSize() {
        var bodyH = document.body.clientHeight;
        var btH = document.getElementById("btnbarDiv").clientHeight;
        document.getElementById("selDepConDiv").style.height = (bodyH - btH - 40) + "px";
      }

      jQuery(window).resize(function () {
        setDivSize();
      });

      window.onload = function () {
        doInit();
        setDivSize();
      }


      /* 设置值到目标文本框中 */
      function resetTargetValue() {
        var targetValue = "";
        var names = "";
        var emails = "";
        jQuery("input[name='_selectitem']:checked").each(function () {
          targetValue += jQuery(this).attr("value") + ";";//多个用户用“;”分隔
          names += jQuery(this).attr("text") + ";";
        });
        targetValue = targetValue.substring(0, targetValue.length - 1);
        names = names.substring(0, names.length - 1);
        //parentObj.document.getElementsByName(targetid)[0].value=targetValue;
        if (parentObj.document.getElementById(targetid)) {
          parentObj.document.getElementById(targetid).value = targetValue;
        }
        if (parentObj.document.getElementById(viewName)) {
          parentObj.document.getElementById(viewName).value = names;
          parentObj.document.getElementById(viewName).title = bulitTitle(names);
        }
        //parentObj.findUserName(targetValue);
      }

      jQuery(document).ready(function () {
        jQuery("input[type='checkbox']").click(function () {
          resetTargetValue();
        });
      });
    </script>
  </head>

  <body style="overflow:hidden;">
    <form name="formList" method="post" action="">
      <tr style="display:none;">
        <td colspan="2">
          <input type="hidden" name="" value="_orderby" />
        </td>
      </tr>

      <div id="btnbarDiv" class="btnbarDiv">
        <div>
          <button type="button" class="button-class" onClick="doReturn();">
            <img src="../images/act_4.gif">确定
          </button>
        </div>
      </div>
      <div id="selDepConDiv" class="selDepConDiv">
        <div id="deplist" class="commFont">
          <script type="text/javascript">
            var contextPath = '/qms';
            var d = new dTree('d', 'deplist');
            //d.config.multiSelect = args.multiSelect != undefined ? args.multiSelect : true;
            d.config.multiSelect = true;
            var topLevel = {};
            var iconPath = "/obpm/magic-static/qms"
            jQuery.ajax({
							// url:contextPath+"/qm/getRolesList.action"+"?selectMode="+selectMode,
							url: "/obpm/api/runtime/__XelBa3DfXZSIOazulYu/departments/selectbox/childs?parentId=",
							async: false,
							type:"get",
							success:function(data){
                if(data.errcode == 0){
                  topLevel = data.data[0];
                  d.add(
                    topLevel.id,
                    '-1',
                    topLevel.name,
                    'javascript:doSelect({value: '+topLevel.id+', text: '+topLevel.name+'});',
                    topLevel.id,
                    '',
                    iconPath + '/images/dept.gif',
                    '',
                    '',
                    ''
                  )
                }
              },
            });
            
            jQuery.ajax({
							// url:contextPath+"/qm/getRolesList.action"+"?selectMode="+selectMode,
							url: "/obpm/api/runtime/department/list",
							async: false,
							type:"get",
							success:function(data){
                if(data.errcode == 0){
                  let list = data.data;
                  list.forEach(function(item){
                    if(item.superior){
                      d.add(
                        item.id,
                        item.superior.id,
                        item.name,
                        'javascript:doSelect({value: '+item.id+', text: '+item.name+'});',
                        item.id,
                        '',
                        iconPath + '/images/dept.gif',
                        iconPath + '/images/dept.gif',
                        '',
                        ''
                      )
                    }
                  })
                }
              },
            });
            document.write(d);
          </script>
        </div>
      </div>
    </form>
  </body>
</o:MultiLanguage>

</html>